package util

import (
	"fmt"
	"github.com/pelletier/go-toml"
	"os"
	config2 "passwordBook/config"
)

func ReadToml(configFilePath string) config2.CommonConfig {
	tomlTree, loadErr := toml.LoadFile(configFilePath)
	if loadErr != nil {
		panic("加载配置文件出错，错误原因：" + loadErr.Error())
	} else {
		config := config2.CommonConfig{}
		umerr := tomlTree.Unmarshal(&config)
		if umerr != nil {
			panic("读取配置文件出错，错误原因：" + umerr.Error())
		} else {
			return config
		}
	}

}
func WriteToml(commonConfig config2.CommonConfig, configFilePath string) {

	data, merr := toml.Marshal(&commonConfig)

	if merr != nil {
		panic("序列化配置文件出错，错误原因：" + merr.Error())
	} else {
		err := os.WriteFile(configFilePath, data, 0777)
		if err != nil {
			fmt.Println("错误：" + err.Error())
			panic("打开配置文件失败，失败原因：" + err.Error())
		}
	}
}
